/**
 * Search
 */

import expired from '@/config/expired'
import * as cache from '@/util/cache'
import md5 from 'js-md5'
const historyKey = 'SS' + md5('history')
const hotKey = 'SS' + md5('hot-list')

export default {
  namespaced: true,
  state: {
    history: null,
    hotList: null
  },
  getters: {
    history: state => {
      if (state.history === null) {
        state.history = cache.get(historyKey)
      }
      return state.history
    },
    hotList: state => {
      if (state.hotList === null) {
        state.hotList = cache.get(hotKey)
      }
      return state.hotList
    }
  },
  mutations: {
    SET_HISTORY: (state, data) => {
      state.history = data
      cache.save(historyKey, data, expired.searchExpired)
    },
    SET_HOT: (state, data) => {
      state.hotList = data
      cache.save(hotKey, data, expired.searchExpired)
    },
    CLEAR_HISTORY: (state) => {
      state.history = []
      cache.remove(historyKey)
    }
  },
  actions: {
    set_history: ({ commit }, data) => {
      commit('SET_HISTORY', data)
    },
    set_host: ({ commit }, data) => {
      commit('SET_HOT', data)
    },
    clear_history: ({ commit }, data) => {
      commit('CLEAR_HISTORY', data)
    }
  }
}
